﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telerik.Windows.Controls;
using System.IO;

namespace HealthManagement.Models
{
    public class ChartModel : ViewModelBase
    {
        private const int timeCycleSeconds = 5;
        private Random r = new Random();
        private Queue<ChartValue> cpuData;
        //private DateTime nowTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);

        private Queue<ChartValue> data;
        private double axisXMinValue;
        private double axisXMaxValue;
        private double axisXStep;

        private int axisYMinValue;
        private int axisYMaxValue;
        private int axisYStep;

        private int _yIntercept;

        private int _samplingThreshold;

        private DateTime preTime;

        public int Freq { get; set; }

        public ChartModel(int freq)
        {
            Freq = freq;
            _samplingThreshold = QueueCapacity;
            cpuData = new Queue<ChartValue>(QueueCapacity);
        }

        public void Reset()
        {
            cpuData.Clear();
            data.Clear();
        }

        public int QueueCapacity
        {
            get
            {
                return this.Freq * timeCycleSeconds;
            }
        }

        public Queue<ChartValue> CpuData
        {
            get
            {
                return cpuData;
            }
        }

        public Queue<ChartValue> Data
        {
            get
            {
                return this.data;
            }
            set
            {
                if (this.data != value)
                {
                    this.data = value;
                    this.OnPropertyChanged("Data");
                }
            }
        }

        public double AxisXMinValue
        {
            get
            {
                return this.axisXMinValue;
            }
            set
            {
                if (this.axisXMinValue != value)
                {
                    this.axisXMinValue = value;
                    this.OnPropertyChanged("AxisXMinValue");

                }
            }
        }

        public double AxisXMaxValue
        {
            get
            {
                return this.axisXMaxValue;
            }
            set
            {
                if (this.axisXMaxValue != value)
                {
                    this.axisXMaxValue = value;
                    this.OnPropertyChanged("AxisXMaxValue");
                }
            }
        }

        public double AxisXStep
        {
            get
            {
                return this.axisXStep;
            }
            set
            {
                if (this.axisXStep != value)
                {
                    this.axisXStep = value;
                    this.OnPropertyChanged("AxisXStep");
                }
            }
        }

        public int AxisYMinValue
        {
            get
            {
                return this.axisYMinValue;
            }
            set
            {
                if (this.axisYMinValue != value)
                {
                    this.axisYMinValue = value;
                    this.OnPropertyChanged("AxisYMinValue");
                }
            }
        }

        public int AxisYMaxValue
        {
            get
            {
                return this.axisYMaxValue;
            }
            set
            {
                if (this.axisYMaxValue != value)
                {
                    this.axisYMaxValue = value;
                    this.OnPropertyChanged("AxisYMaxValue");
                }
            }
        }

        public int AxisYStep
        {
            get
            {
                return this.axisYStep;
            }
            set
            {
                if (this.axisYStep != value)
                {
                    this.axisYStep = value;
                    this.OnPropertyChanged("AxisYStep");
                }
            }
        }

        public int YIntercept
        {
            get
            {
                return _yIntercept;
            }
            set
            {
                if (this._yIntercept != value)
                {
                    this._yIntercept = value;
                    this.OnPropertyChanged("YIntercept");
                }
            }
        }

        public int SamplingThreshold
        {
            get
            {
                return _samplingThreshold;
            }
            set
            {
                if (this._samplingThreshold != value)
                {
                    this._samplingThreshold = value;
                    this.OnPropertyChanged("SamplingThreshold");
                }
            }
        }

        public void EnqueueValue(IEnumerable<int> src, DateTime now)
        {
            if (preTime == default(DateTime))
                preTime = now.AddMilliseconds(-1000.0 / Freq * (src.Count() - 1));

            int idx = 1;
            foreach (var i in src)
            {
                if (this.cpuData.Count >= QueueCapacity)
                {
                    cpuData.Dequeue();
                }
                cpuData.Enqueue(new ChartValue() { Value = i, Time = preTime.AddMilliseconds((now - preTime).TotalMilliseconds / src.Count() * idx++) });
            }
            this.preTime = now;
        }

        public void SetUpAxisXRange(DateTime now)
        {
            if (this.cpuData.Count == QueueCapacity)
            {
                this.AxisXMinValue = now.AddSeconds(-timeCycleSeconds - 0.1).ToOADate();
                this.AxisXMaxValue = now.AddSeconds(0.1).ToOADate();
            }
            else
            {
                this.AxisXMinValue = now.AddSeconds(-timeCycleSeconds - 0.1).ToOADate();
                this.AxisXMaxValue = now.AddSeconds(0.1).ToOADate();
                //this.AxisXStep = 1.0 / 24.0 / 3600.0 / 10  ;
            }

        }


    }

}
